package JDBC;

import java.sql.*;

public class DbOp {
        private static Connection conn;
        private static Statement stmt;
        private static String driver = "com.mysql.cj.jdbc.Driver";
        private static String url = "jdbc:mysql://localhost:3306/people";
        private static String dbUser = "root";
        private static String dbPass = "991218zf";

        public static void getConn() {
            try {
                Class.forName(driver);
                if(conn==null) {
                    conn = DriverManager.getConnection(url,dbUser,dbPass);
                }
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }

        public static void getStmt() {
            if(conn==null) {
                getConn() ;
            }
            try {
                stmt = conn.createStatement();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }

        public static ResultSet getQuery(String sql) {
            ResultSet rs=null;
            if(stmt==null) {
                getStmt();
            }
            try {
                rs = stmt.executeQuery(sql);
            } catch (SQLException e) {

                e.printStackTrace();
            }
            return rs;
        }

        public static boolean getUpdate(String sql) {
            boolean b = false;
            int rs = -1;
            if(stmt==null) {
                getStmt();
            }
            try {
                rs = stmt.executeUpdate(sql);
                b = true;
            } catch (SQLException e) {

                e.printStackTrace();
            }
            return b;
        }
}
